package com.flowersinc.shop.views
{
	import com.flowersinc.shop.model.Flower;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.controls.Button;
	import mx.controls.DataGrid;
	import mx.controls.Image;
	import mx.core.DragSource;
	import mx.events.CloseEvent;
	import mx.managers.DragManager;
	import mx.managers.PopUpManager;

	public class FlowersViewBase extends Canvas
	{
		
		[Bindable]	
		public function get flowers() : ArrayCollection 
		{
			return _flowers;
		}
		
		public function set flowers(value : ArrayCollection) : void 
		{
			_flowers = value;
			
			for each (var flower : Flower in value) {
				flower.addEventListener("orderPlaced", onOrderPlaced); // to make every flower in dataGrid listener 
				flower.addEventListener("infoShowed", onInfoShowed);
			}
		} 
		
		public function get selectedFlower() : Flower 
		{
			return _selectedFlower;
		}
		
		public function set selectedFlower(value : Flower) : void 
		{
			_selectedFlower = value;
			flowersDataGrid.selectedItem = selectedFlower;
		}
		
		[Bindable]
		public var flowersDataGrid : DataGrid;
		public var shoppingChart : ShoppingCartBase;
		
		protected function onGridMouseDown(event : MouseEvent) : void 
		{
			var flower : Flower = Flower(flowersDataGrid.selectedItem);
			
			var image : Image = new Image();
			image.source = flower.thumbImage;
			image.width = 70;
			image.height = 70;
				
			var dragSource : DragSource = new DragSource();
			dragSource.addData(flower, "flower");
			
            var offsetx : int = -flowersDataGrid.contentMouseX - 15;
            var offsety : int = -flowersDataGrid.contentMouseY - 15;
	  
	  		DragManager.doDrag(flowersDataGrid, dragSource, event, image, offsetx, offsety, 0.8);		
		}
		
		private function onOrderPlaced (event : Event) : void {
			shoppingChart.addToCart(Flower(event.target));	
		}
			
		private function onInfoShowed(event : Event) : void 
		{
			if (getPopUpByFlower(Flower(flowersDataGrid.selectedItem)) == null) 
			{
				var popUp : PopUpWindow = PopUpManager.createPopUp(this, PopUpWindow, false) as PopUpWindow;
				PopUpManager.centerPopUp(popUp);
				popUp.addEventListener(Event.CLOSE, onPopUpClose);
				popUp.addEventListener("flowerOrdered", onFlowerOrdered)
				popUp.flower = Flower(flowersDataGrid.selectedItem);
				
				_currentPopUps.addItem(popUp);
			}
		}	
		
		private function onPopUpClose(event : CloseEvent):void 
		{
			PopUpManager.removePopUp(PopUpWindow(event.currentTarget));
			_currentPopUps.removeItemAt(_currentPopUps.getItemIndex(event.currentTarget));
		}
		
		private function onFlowerOrdered(event : Event) : void 
		{
			shoppingChart.addToCart(PopUpWindow(event.currentTarget).flower);
			PopUpManager.removePopUp(PopUpWindow(event.currentTarget));
			_currentPopUps.removeItemAt(_currentPopUps.getItemIndex(event.currentTarget));
		}
		
		private function getPopUpByFlower(flower : Flower) : PopUpWindow 
		{
			for each (var item : PopUpWindow in _currentPopUps) 
			{
				if (item.flower == flower) 
				{
					return item;
				}
			}
			
			return null;
		}		
		
		private var _flowers : ArrayCollection;
		private var _selectedFlower : Flower;
		private var _currentPopUps : ArrayCollection = new ArrayCollection();
	}
}